from sqlalchemy import select
from forward.dependencies.database import db
from forward.model.schemas.lucky.merchant import MerchantSchema
from forward.model.tables.activity import ActivityModel
from forward.model.tables.merchant import MerchantModel


class MerchantORM:
    @classmethod
    async def get_one_by_id(cls, id: int) -> MerchantSchema | None:
        statement = select(MerchantModel).where(MerchantModel.id == id)
        recode = await db.fetch_one(statement)
        if not recode:
            return None
        return MerchantSchema.model_validate(recode, from_attributes=True)

    @classmethod
    async def get_one_by_activity_id(cls, id: int) -> MerchantSchema | None:
        statement = (
            select(MerchantModel)
            .select_from(MerchantModel)
            .join(ActivityModel, ActivityModel.merchant_id == MerchantModel.id)
            .where(ActivityModel.id == id)
        )
        recode = await db.fetch_one(statement)
        if not recode:
            return None
        return MerchantSchema.model_validate(recode, from_attributes=True)
